package cn.com.org.service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import cn.com.common.EncryptUtil;
import cn.com.common.tools.CookieUtil;

@Component
public class Common {

	 @Autowired
   private RedisTemplate redisTemplate;
	 @Autowired
	 private StringRedisTemplate sredisTemplate;
	 /**
	  * set redis value
	  * @param key
	  * @param value
	  * @param expire
	  * @return
	  */
	 
	 public String setRedisData(String key,String value,Integer expire){	
	    ValueOperations<String,String> valueoption=redisTemplate.opsForValue();
	     String rvalue=valueoption.get(key);
	     if(rvalue!=null){
	    	 sredisTemplate.expire(key, 30, TimeUnit.MINUTES);
	    	 return rvalue;
	     }	 
	     rvalue=value;
        valueoption.set(key, value,30,TimeUnit.MINUTES);
		return rvalue;
	}
	/**
	 * get redis value by redis key
	 * @param key
	 * @return value result
	 */
	public String getRedisValue(String key){
		 ValueOperations<String,String> valueoption=redisTemplate.opsForValue();
	     String result=valueoption.get(key);
	     return result;
	}
	/**
	 * validate redis value (only redis value)
	 * @param key
	 * @param value
	 * @return if true expire 30 minutes,else return
	 */
	public Boolean validateUserinfo(String key,String value){
		 ValueOperations<String,String> valueoption=redisTemplate.opsForValue();
		 String result=getRedisValue(key);
		 if(value.equals(result)){
			 sredisTemplate.expire(key, 30, TimeUnit.MINUTES);
			 return true;
		 }
		 return false;
	}
	public String encodeKey(String key){	
		LocalDateTime ldt=LocalDateTime.now();
	    String datekey=ldt.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
	    
		String result=EncryptUtil.EncryptMd5OrSHA(datekey+key,"MD5");
		return result;
	}
	public Boolean validateUser(String uname,String accesskey){
		
		return true;
	}
	
	/**
	 * set redis key:name value:accessKey
	 * @param uname
	 * @param accessKey
	 * @param expire
	 * @return
	 */
	 public String setUserRedisData(String uname,String accessKey,Integer expire){			 
		    ValueOperations<String,String> valueoption=redisTemplate.opsForValue();
		     String rvalue=valueoption.get(uname);
		     if(rvalue!=null){
		    	 sredisTemplate.expire(uname,expire, TimeUnit.MINUTES);
		    	 return rvalue;
		     }	 
		     
	         valueoption.set(uname, accessKey,expire,TimeUnit.MINUTES);
			return accessKey;
		}
	 public void addUserCookie(HttpServletResponse response,String uname){
		    CookieUtil.setCookie(response, "lcysite_name", uname, -1);
		    String accText=EncryptUtil.EncodeBase64(UUID.randomUUID().toString());
		    accText=setUserRedisData(uname,accText,30);
			CookieUtil.setCookie(response, "lcysite_accesskey", accText, -1);
			
	 }
	 public Boolean clearUserCookie(HttpServletRequest request,HttpServletResponse response){
		 CookieUtil.delAllCookie(request, response);
		 //CookieUtil.delCookie(response,"lcname");		 
		 return true;
	 }
	 /**
	  * validate user login state
	  * @param request
	  * @return
	  */
	public Boolean validateUserLogState(HttpServletRequest request){
	      String uname=	CookieUtil.getCookie(request,"lcysite_name");
	      String accessKey=CookieUtil.getCookie(request, "lcysite_accesskey");
	      if(uname.isEmpty()||accessKey.isEmpty()){
	    	  return false;
	      }
	       return validateUserinfo(uname, accessKey);
	}

}
